<?php

namespace app\api\controller;


use app\api\service\GameService;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use app\common\Error;
use app\common\Common;

class Weixin extends Base
{

    private $service;

    public function __construct(GameService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    /**
     * 获取首页数据
     *
     * @param array params
     * @param int page limit
     * @return json
     */
    public function index()
    {

        return self::getStableAccessToken();

    }

    /**
     * 获取首页数据
     *
     * @param array params
     * @param int page limit
     * @return json
     */
    public function getminigamecpslist($next_offset = '')
    {

        $accessToken = self::getStableAccessToken();
        $url = 'https://api.weixin.qq.com/wxa/servicemarket/game/getminigamecpslist?access_token=' . $accessToken;
        $params['offset'] = $next_offset;
        $data = self::curlRequest($url, 'POST', json_encode($params));
        $result = json_decode($data, true);

        if ($result['errcode'] != 0) {
            return Common::error($result['errmsg']);
        } else {
            foreach ($result['app_list'] as $key => $value) {
                $game = Db::name('game')->where('appid', $value['appid'])->find();
                if (empty($game)) {
                    Db::name('game')->insert([
                        'appid' => $value['appid'], 'appname' => $value['appname']
                        , 'brief_intro' => $value['brief_intro'], 'logo' => $value['logo'],
                        'category_2_id' => $value['category_2_id'], 'category_2_name' => $value['category_2_name'],
                        'category_3_id' => $value['category_3_id'], 'category_3_name' => $value['category_3_name'],
                        'user_name' => $value['user_name'], 'banner' => isset($value['banner']) ? $value['banner'] : '',
                        'publicity_img' => json_encode($value['publicity_img']), 'logo_square' => $value['logo_square'],
                        'cps_percent_new_user_cps_percent' => $value['cps_percent']['new_user_cps_percent'], 'cps_percent_returning_user_cps_percent' => $value['cps_percent']['returning_user_cps_percent'],
                        'created_at' => time(), 'updated_at' => time()


                    ]);

                } else {
                    Db::name('game')->where('appid', $value['appid'])->update([
                        'appname' => $value['appname']
                        , 'brief_intro' => $value['brief_intro'], 'logo' => $value['logo'],
                        'category_2_id' => $value['category_2_id'], 'category_2_name' => $value['category_2_name'],
                        'category_3_id' => $value['category_3_id'], 'category_3_name' => $value['category_3_name'],
                        'user_name' => $value['user_name'], 'banner' => isset($value['banner']) ? $value['banner'] : '',
                        'publicity_img' => json_encode($value['publicity_img']), 'logo_square' => $value['logo_square'],
                        'cps_percent_new_user_cps_percent' => $value['cps_percent']['new_user_cps_percent'], 'cps_percent_returning_user_cps_percent' => $value['cps_percent']['returning_user_cps_percent'],
                        'updated_at' => time()


                    ]);

                }


            }

            self::getminigamecpslist($result['next_offset']);
        }


    }


    /**
     * 获取首页数据
     *
     * @param array params
     * @param int page limit
     * @return json
     */
    public function wxgamelist()
    {
        $params = Request::param();
        $data = $this->service->getGameList($params);
        return Common::success($data);


    }

}